package com.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.system.UserTempInfo;
import com.org.service.UserTempService;

@Controller
@RequestMapping("/userTemp")
public class UserTempAction {
	/**
	 * 用户信息初始化，添加用户，删除用户
	 */
	@Autowired
	private UserTempService userTempService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<UserTempInfo> userTempInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (userTempInfo.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", userTempInfo);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	private Map<String, Object> status(Boolean userTempInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (userTempInfo) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getUserInfos() {
		List<UserTempInfo> userTempInfo = new ArrayList<UserTempInfo>();
		userTempInfo = userTempService.findAll();
		return status(userTempInfo);
	}

	@RequestMapping("/{userId}/findById")
	public @ResponseBody Map<String, Object> getUserInfo(
			@PathVariable int userId) {
		List<UserTempInfo> userTempInfo = new ArrayList<UserTempInfo>();
		userTempInfo = userTempService.findById(userId);
		return status(userTempInfo);
	}

	@RequestMapping("/addUser")
	public @ResponseBody Map<String, Object> addUser(
			@RequestBody Map<String, Object> userMap) {
		boolean result;
		iter = userMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		UserTempInfo userTempInfo = new UserTempInfo();
		userTempInfo.setUserName((String) userMap.get(list.get(0)));
		result = userTempService.addUser(userTempInfo);
		return status(result);
	}

	@RequestMapping("/updateUser")
	public @ResponseBody Map<String, Object> updateUser(
			@RequestBody Map<String, Object> userMap) {
		boolean result;
		iter = userMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		UserTempInfo userTempInfo = new UserTempInfo();
		userTempInfo.setUserId((Integer) userMap.get(list.get(0)));

		result = userTempService.updateUser(userTempInfo);
		return status(result);
	}
}
